package com.mumu.xiaodu

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class BookmarkAdapter(
    private val onBookmarkClick: (Bookmark) -> Unit,
    private val onAddBookmarkClick: (() -> Unit)? = null
) : RecyclerView.Adapter<BookmarkAdapter.BookmarkViewHolder>() {
    
    private var bookmarks = mutableListOf<Bookmark>()
    
    fun updateBookmarks(newBookmarks: List<Bookmark>) {
        bookmarks.clear()
        bookmarks.addAll(newBookmarks)
        notifyDataSetChanged()
    }
    
    fun getCurrentBookmarks(): List<Bookmark> {
        return bookmarks.toList()
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarkViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_bookmark, parent, false)
        return BookmarkViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: BookmarkViewHolder, position: Int) {
        if (position < bookmarks.size) {
            holder.bind(bookmarks[position])
        } else {
            // 显示"添加书签"按钮
            holder.bindAddButton()
        }
    }
    
    override fun getItemCount(): Int {
        return bookmarks.size + 1 // 包含添加按钮
    }
    
    inner class BookmarkViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivIcon: ImageView = itemView.findViewById(R.id.ivIcon)
        private val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
        private val tvUrl: TextView = itemView.findViewById(R.id.tvUrl)
        private val ivBookmarkFlag: ImageView = itemView.findViewById(R.id.ivBookmarkFlag)
        
        fun bind(bookmark: Bookmark) {
            tvTitle.text = bookmark.title
            tvUrl.text = getDomainFromUrl(bookmark.url)
            
            // 设置网站图标和颜色
            setWebsiteIcon(bookmark)
            
            // 显示书签标识
            ivBookmarkFlag.visibility = View.VISIBLE
            
            itemView.setOnClickListener {
                onBookmarkClick(bookmark)
            }
            
            // 设置焦点样式
            itemView.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) {
                    itemView.setBackgroundResource(R.drawable.bookmark_item_focused)
                } else {
                    itemView.setBackgroundResource(R.drawable.bookmark_item_normal)
                }
            }
        }
        
        fun bindAddButton() {
            tvTitle.text = "添加书签"
            tvUrl.text = "点击添加新的网站"
            ivIcon.setImageResource(R.drawable.ic_add)
            ivBookmarkFlag.visibility = View.GONE
            
            // 设置虚线边框样式
            itemView.setBackgroundResource(R.drawable.add_bookmark_item)
            
            itemView.setOnClickListener {
                onAddBookmarkClick?.invoke()
            }
        }
        
        private fun setWebsiteIcon(bookmark: Bookmark) {
            when {
                bookmark.url.contains("baidu.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_baidu)
                }
                bookmark.url.contains("google.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_google)
                }
                bookmark.url.contains("youtube.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_youtube)
                }
                bookmark.url.contains("iqiyi.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_iqiyi)
                }
                bookmark.url.contains("qq.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_tencent)
                }
                bookmark.url.contains("youku.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_youku)
                }
                bookmark.url.contains("bilibili.com") -> {
                    ivIcon.setImageResource(R.drawable.ic_bilibili)
                }
                else -> {
                    ivIcon.setImageResource(R.drawable.ic_website_default)
                }
            }
        }
        
        private fun getDomainFromUrl(url: String): String {
            return try {
                val domain = url.replace("https://", "").replace("http://", "")
                domain.split("/")[0]
            } catch (e: Exception) {
                url
            }
        }
    }
}

data class Category(
    val name: String,
    val type: CategoryType,
    var isSelected: Boolean
)

enum class CategoryType {
    BOOKMARKS,
    SEARCH_ENGINES,
    VIDEO_SITES,
    COMMON_SITES,
    HISTORY
} 